/*
 * database manager interfaces
 */

#include "dbm.h"
#include "log.h"
#include "zm_spec.h"

static char *perrmsg = NULL;
static sqlite3 *db = NULL;
static char sqlcmd[SQL_CMD_LEN]={0};
static char textinfo[LOG_BUF_LEN]={0};

int db_init(const char *dbname,const char *table)
{
	if(dbname == NULL || table == NULL)
	{
		dzlog_error("invaid params!\n");
		return -1;
	}
	if(sqlite3_open(dbname,&db)!=SQLITE_OK)
	{
		dzlog_error("open %s error:%s\n",dbname,sqlite3_errmsg(db));
		return -1;
	}
	// create history table
	snprintf(sqlcmd,SQL_CMD_LEN,CREATE_HIST_TABLE_FMT,HISTORY_TABLE_NAME);
	sqlite3_exec(db,sqlcmd,0,0,NULL);

	// delete table before create it if has
	snprintf(sqlcmd,SQL_CMD_LEN,DELETE_TABLE_FMT,table);
	sqlite3_exec(db,sqlcmd,0,0,0);
	snprintf(sqlcmd,SQL_CMD_LEN,CREATE_IPC_TABLE_FMT,table);
	if(sqlite3_exec(db,sqlcmd,0,0,&perrmsg)!=SQLITE_OK)
	{
		dzlog_error("create table %s error:%s\n",table,perrmsg);
		sqlite3_free(perrmsg);
		return -1;
	}

	return 0;
}

int db_insert_ipc(const char *table, const void *data, unsigned int type)
{
	if(table == NULL || data == NULL)
	{
		dzlog_error("invalid params!\n");
		return -1;
	}

	int ret = 0;
	char *flag = NULL;
	
	if(type == SPEC_ZM)
	{
		Device_Stat_Info *pinfo = (Device_Stat_Info*)data;
		// for log
		ret += snprintf(textinfo, LOG_BUF_LEN,"DEV_ID:%s,MAC:%s,Ver:%s,",pinfo->dev_id,pinfo->macaddr,pinfo->version);
		ret += snprintf(textinfo+ret,LOG_BUF_LEN-ret,"ip:%s,subnet:%s,defgw:%s,dnsip:%s,",pinfo->ipaddrs.dev_ip,pinfo->ipaddrs.subnet,pinfo->ipaddrs.defgw,pinfo->ipaddrs.dnsip);
		flag = (pinfo->port_info.dhcp_flag==DHCP_ON)? "on":"off";
		ret += snprintf(textinfo+ret,LOG_BUF_LEN-ret, "dhcp:%s,web_port:%d,video_port:%d\n",flag,pinfo->port_info.web_port, pinfo->port_info.video_port);
		dzlog_debug("insert into %s: %s\n",table, textinfo);
		// insert into table
		snprintf(sqlcmd,SQL_CMD_LEN,INSERT_IPC_TABLE_FMT,table,
				pinfo->dev_id,pinfo->version,pinfo->macaddr,
				pinfo->port_info.web_port,pinfo->port_info.video_port,flag,
				pinfo->ipaddrs.dev_ip,pinfo->ipaddrs.subnet,pinfo->ipaddrs.defgw,pinfo->ipaddrs.dnsip,1);
		ret = sqlite3_exec(db,sqlcmd,0,0,&perrmsg);
		if(ret != SQLITE_OK)
		{
			dzlog_error("insert line error:%s\n",perrmsg);
			sqlite3_free(perrmsg);
			return -1;
		}
	}
	else
	{
		dzlog_error("not supported spec type now!\n");
		return -1;
	}

	return 0;
}

int db_insert_hist(const char *table, const void *data, unsigned int type)
{
	if(table == NULL || data == NULL)
	{
		dzlog_error("invalid params!\n");
		return -1;
	}

	int ret = 0;
	if(type == SPEC_ZM)
	{
		Device_Stat_Info *pinfo = (Device_Stat_Info*)data;
		// insert into table
		char *flag = pinfo->port_info.dhcp_flag==DHCP_ON?"on":"off";
		snprintf(sqlcmd,SQL_CMD_LEN,INSERT_HIST_TABLE_FMT,table,
				pinfo->dev_id,pinfo->version,pinfo->macaddr,
				pinfo->port_info.web_port,pinfo->port_info.video_port,flag,
				pinfo->ipaddrs.dev_ip,pinfo->ipaddrs.subnet,pinfo->ipaddrs.defgw,pinfo->ipaddrs.dnsip,1);
		ret = sqlite3_exec(db,sqlcmd,0,0,&perrmsg);
		if(ret != SQLITE_OK)
		{
			dzlog_error("insert line error:%s\n",perrmsg);
			sqlite3_free(perrmsg);
			return -1;
		}
	}
	else
	{
		dzlog_error("not supported spec type now!\n");
		return -1;
	}

	return 0;
}

void db_exit(void)
{
	sqlite3_close(db);
	db = NULL;
}

int db_select_callback(void * data, int col_count, char ** col_values, char ** col_name)
{
	int i;

	for(i=0;i<col_count;i++)
	{
		printf("%s  ",col_name[i]);
	}
	printf("\n");
	for(i=0;i<col_count;i++)
	{
		printf("%s  ",col_values[i]);
	}
	printf("\n");

	return 0;
}

